package com.gf.forcesystem.controller;

import com.gf.forcesystem.api.WeatherManager;
import com.gf.forcesystem.config.CotaskingRabbitmqConfig;
import com.gf.forcesystem.model.dto.weather.*;
import com.gf.forcesystem.api.WeatherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.C;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @author gaofei
 * @date 2022/6/21 19:49
 */
@RestController
@Api(tags = "天气模拟接口")
@RequestMapping("weather")
@Slf4j
public class WeatherController implements MessagePostProcessor {
    @Resource
    private List<WeatherService> weatherServices;

    @Autowired
    private WeatherManager weatherManager;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @ApiOperation("获取当前的天气信息")
    @GetMapping("nowweather")
    public Map<String, AbstractWeather> nowWeather() {
        List<AbstractWeather> abstractWeathers = weatherManager.getWeathers();
        Map<String, AbstractWeather> weatherMap = new HashMap<>();
        abstractWeathers.forEach(weather -> weatherMap.put(weather.getMessageType(), weather));
        return weatherMap;
    }

    @ApiOperation("模拟雾")
    @PostMapping("simulationFog")
    public Boolean simulationFog(@RequestBody Fog fog) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(11);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.put((byte) 0x4A);
        byteBuffer.put((byte) 0x58);
        byteBuffer.put((byte) 0x0B);
        byteBuffer.put((byte) 0x00);
        byteBuffer.put((byte) 0xC5);
        byteBuffer.putShort(fog.getType());
        rabbitTemplate.convertAndSend(CotaskingRabbitmqConfig.weatherExchangeName, CotaskingRabbitmqConfig.weatherRoutingKey,
                byteBuffer.array());
        log.info("收到模拟雾命令 {}", fog);
        return true;
    }

    @PostMapping("simulationRain")
    @ApiOperation("模拟雨")
    public Boolean simulationRain(@RequestBody Rain rain) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(11);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.put((byte) 0x4A);
        byteBuffer.put((byte) 0x58);
        byteBuffer.put((byte) 0x0B);
        byteBuffer.put((byte) 0x00);
        byteBuffer.put((byte) 0xC2);
        byteBuffer.put(rain.getType());
        rabbitTemplate.convertAndSend(CotaskingRabbitmqConfig.weatherExchangeName, CotaskingRabbitmqConfig.weatherRoutingKey,
                byteBuffer.array(), this);

        log.info("收到模拟雨命令 {}", rain);
        return true;
    }

    @PostMapping("simulationSandDust")
    @ApiOperation("模拟沙尘")
    public Boolean simulationSandDust(@RequestBody SandDust sandDust) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(11);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.put((byte) 0x4A);
        byteBuffer.put((byte) 0x58);
        byteBuffer.put((byte) 0x0B);
        byteBuffer.put((byte) 0x00);
        byteBuffer.put((byte) 0xC6);
        byteBuffer.put(sandDust.getType());
        rabbitTemplate.convertAndSend(CotaskingRabbitmqConfig.weatherExchangeName, CotaskingRabbitmqConfig.weatherRoutingKey,
                byteBuffer.array(), this);
        log.info("收到模拟沙尘命令 {}", sandDust);
        return true;
    }

    @PostMapping("simulationSimulationTime")
    @ApiOperation("模拟时间")
    public Boolean simulationSimulationTime(@RequestBody SimulationTime simulationTime) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(11);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.put((byte) 0x4A);
        byteBuffer.put((byte) 0x58);
        byteBuffer.put((byte) 0x0B);
        byteBuffer.put((byte) 0x00);
        byteBuffer.put((byte) 0xC0);
        byteBuffer.put((byte)(simulationTime.getTime() & 0xFF));
        byteBuffer.putShort(simulationTime.getTimeCoefficient());

        rabbitTemplate.convertAndSend(CotaskingRabbitmqConfig.weatherExchangeName, CotaskingRabbitmqConfig.weatherRoutingKey,
                byteBuffer.array(), this);
        log.info("收到模拟时间命令 {}", simulationTime);
        return true;
    }

    @PostMapping("simulationSnow")
    @ApiOperation("模拟雪")
    public Boolean simulationSnow(@RequestBody Snow snow) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(11);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.put((byte) 0x4A);
        byteBuffer.put((byte) 0x58);
        byteBuffer.put((byte) 0x0B);
        byteBuffer.put((byte) 0x00);
        byteBuffer.put((byte) 0xC4);
        byteBuffer.put(snow.getType());
        rabbitTemplate.convertAndSend(CotaskingRabbitmqConfig.weatherExchangeName, CotaskingRabbitmqConfig.weatherRoutingKey,
                byteBuffer.array(), this);
        log.info("收到模拟雪命令 {}", snow);
        return true;
    }

    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
        message.getMessageProperties().setExpiration("5000");
        return message;
    }
}
